﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using BrnMall.Manager.Domain.Promotion;
using PetaPoco;

namespace BrnMall.Manager.Manager.Promotion
{
    /// <summary>
    /// 团购活动
    /// </summary>
    public static class GroupBuyManager
    {
        #region 拼团活动
        public static void InsertGroupBuy(GroupBuy groupBuy)
        {
            using (var db = new QDMallDatabase())
            {
                db.Insert(groupBuy);
            }
        }

        public static void UpdateGroupBuy(GroupBuy groupBuy)
        {
            using (var db = new QDMallDatabase())
            {
                db.Update(groupBuy);
            }
        }
        public static void UpdateGroupBuyStatus(int id,int status)
        {
            using (var db = new QDMallDatabase())
            {
                db.Execute("update Promotion_GroupBuy set status=@0 where id=@1", status, id);
            }
        }

        public static GroupBuy GetGroupBuy(int id)
        {
            using (var db = new QDMallDatabase())
            {
                return db.SingleOrDefault<GroupBuy>(id);
            }
        }

        public static void DeleteGroupBuy(int id)
        {
            DeleteGroupBuyProductByGroupId(id);
            using (var db = new QDMallDatabase())
            {
                db.Delete<GroupBuy>(id);
            }
            
        }

        public static IList<GroupBuy> GetGrouBuyList(int storeId)
        {
            using (var db = new QDMallDatabase())
            {
                return db.Fetch<GroupBuy>("select * from Promotion_GroupBuy where storeid=@0 order by id desc", storeId);
            }
        }
        #endregion

        #region 拼团商品

        public static void AddOrEditGroupBuyProduct(GroupBuyProduct groupBuyProduct)
        {
            using (var db = new QDMallDatabase())
            {
                if(groupBuyProduct.Id==0)
                db.Insert(groupBuyProduct);
                else
                {
                    db.Update(groupBuyProduct);
                }
            }
        }

        public static GroupBuyProduct GetGroupBuyProductInfo(int storeId, int pid)
        {
            using (var db = new QDMallDatabase())
            {
                return db.SingleOrDefault<GroupBuyProduct>(
                    "select top 1 * from Promotion_GroupBuyProduct where storeid=@0 and pid=@1", storeId, pid);
            }
        }

        public static void DeleteGroupBuyProduct(int id)
        {
            using (var db = new QDMallDatabase())
            {
                db.Delete<GroupBuyProduct>(id);
            }
        }
        public static void DeleteGroupBuyProductByGroupId(int groupId)
        {
            using (var db = new QDMallDatabase())
            {
                db.Execute("delete from Promotion_GroupBuyProduct where groupid=@0", groupId);
            }
        }

        public static IList<GroupBuyProduct> GetGroupBuyProducts(int groupId)
        {
            using (var db = new QDMallDatabase())
            {
                return db.Fetch<GroupBuyProduct>("select * from Promotion_GroupBuyProduct where groupid=@0", groupId);
            }
        }

        public static Page<GroupBuyProduct> GetStoreGroupBuyProducts(int storeId,long page=1,long itemsPerPage=15)
        {
            using (var db = new QDMallDatabase())
            {
                var sql=new Sql();
                var newsql=new StringBuilder();
                newsql.Append(
                    "select a.* from Promotion_GroupBuyProduct a left join Promotion_GroupBuy b on a.GroupId=b.id ");
                newsql.AppendFormat(" where a.storeid={1} and DATEDIFF(day,b.StartTime,'{0:yyyy-MM-dd HH:mm:ss}')>=0 and DATEDIFF(day,b.EndTime,'{0:yyyy-MM-dd HH:mm:ss}')<=0",
                    DateTime.Now,storeId);
                return db.Page<GroupBuyProduct>(page,itemsPerPage, newsql.ToString());
            }
        }

        public static GroupBuyProduct GetGroupBuyProduct(int id)
        {
            using (var db = new QDMallDatabase())
            {
                return db.SingleOrDefault<GroupBuyProduct>(id);
            }
        }

        #endregion

        #region 团购设置

        public static void AddGroupBuySetting(GroupBuySetting setting)
        {
            using (var db = new QDMallDatabase())
            {
                db.Insert(setting);
            }
        }

        public static void UpdateGroupBuySetting(GroupBuySetting setting)
        {
            using (var db = new QDMallDatabase())
            {
                db.Update(setting);
            }
        }

        public static GroupBuySetting GetGroupBuySettingByStoreId(int storeId)
        {
            using (var db = new QDMallDatabase())
            {
                return db.SingleOrDefault<GroupBuySetting>("select * from Promotion_GroupBuySetting where storeid=@0 ",
                    storeId);
            }
        }

        public static GroupBuySetting GetGroupBuySetting(int id)
        {
            using (var db = new QDMallDatabase())
            {
                return db.SingleOrDefault<GroupBuySetting>(id);
            }
        }

        #endregion

    }
}
